package com.yupi.springbootinit.BiMQ.BiMq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

public class BiMain {
    public static void main(String[] args) throws Exception {
       createQueue1();
//       createQueue2();
    }

    private static void createQueue2() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //2：指定连接服务的主机名以及账号密码
        factory.setHost("47.97.80.75");
        factory.setUsername("lfc123456");
        factory.setPassword("lfc123456");
        //3：创建连接
        Connection connection = factory.newConnection();
        //4：创建频道
        Channel channel = connection.createChannel();

        channel.exchangeDelete(BIMQConstant.AI_EXCHANGE_NAME);
        //5：声明交换机，名称以及类型
        channel.exchangeDeclare(BIMQConstant.AI_EXCHANGE_NAME,"direct");
        HashMap<String, Object> arguments = new HashMap<>();
        //指定死信队列名称
//        arguments.put("x-dead-letter-exchange", BIMQConstant.AI_DLX_EXCHANGE_NAME);
//        arguments.put("x-dead-letter-routing-key", BIMQConstant.DLX_ROUTING_KEY);
        //6:创建两个队列
        channel.queueDeclare(BIMQConstant.AI_QUEUE_NAME,true,false,false,arguments);
        //7：绑定这个队列在那个交换机上
        channel.queueBind(BIMQConstant.AI_QUEUE_NAME,BIMQConstant.AI_EXCHANGE_NAME,BIMQConstant.AI_ROUTING_KEY);
    }

    private static void createQueue1() throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        //2：指定连接服务的主机名以及账号密码
        factory.setHost("47.97.80.75");
        factory.setUsername("lfc123456");
        factory.setPassword("lfc123456");
        //3：创建连接
        Connection connection = factory.newConnection();
        //4：创建频道
        Channel channel = connection.createChannel();

        channel.exchangeDelete(BIMQConstant.BI_EXCHANGE_NAME);
        //5：声明交换机，名称以及类型
        channel.exchangeDeclare(BIMQConstant.BI_EXCHANGE_NAME,"direct");
        HashMap<String, Object> arguments = new HashMap<>();
        //指定死信队列名称
        arguments.put("x-dead-letter-exchange", BIMQConstant.BI_DLX_EXCHANGE_NAME);
        arguments.put("x-dead-letter-routing-key", BIMQConstant.DLX_ROUTING_KEY);
        //6:创建两个队列
        channel.queueDeclare(BIMQConstant.BI_QUEUE_NAME,true,false,false,arguments);
        //7：绑定这个队列在那个交换机上
        channel.queueBind(BIMQConstant.BI_QUEUE_NAME,BIMQConstant.BI_EXCHANGE_NAME,BIMQConstant.ROUTING_KEY);
    }
}
